package org.petstore.web.servlet;

import org.petstore.domain.Account;
import org.petstore.util.Cart;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import static org.petstore.web.servlet.util.ServletUtil.LOGIN_FORM;

public class viewNewOrderFormServlet extends HttpServlet {
    private static final String CONFIRM_ORDER = "/WEB-INF/jsp/order/ConfirmOrder.jsp";
    private static final String LIST_ORDERS = "/WEB-INF/jsp/order/ListOrders.jsp";
    private static final String NEW_ORDER = "/WEB-INF/jsp/order/NewOrderForm.jsp";
    private static final String SHIPPING = "/WEB-INF/jsp/order/ShippingForm.jsp";
    private static final String VIEW_ORDER = "/WEB-INF/jsp/order/ViewOrder.jsp";
    private static final String ERROR = "/WEB-INF/jsp/common/Error.jsp";

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();
        Account account = (Account) session.getAttribute("loginUser");
        Cart cart = (Cart) session.getAttribute("cart");
        //Order order = new Order();

        if (account == null) {

            request.setAttribute("msg", "请先登录");
            request.getRequestDispatcher(LOGIN_FORM).forward(request, response);

        } else if (cart != null) {
            //order.initOrder(account, cart);
            //session.setAttribute("order", order);
            request.getRequestDispatcher(NEW_ORDER).forward(request, response);
        } else {
            request.setAttribute("message", "购物车为空");
            request.getRequestDispatcher(ERROR).forward(request, response);
        }
    }
}
